﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

/// <summary>
///Login 的摘要说明
/// </summary>
public class Login
{
    private SqlCommand cmd;

	public Login()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}

    public string LoginSys(TBperson per,string clname)
    {
        string result = "0";
        try
        {
            cmd = new SqlCommand("select count(*) from tb_person,tb_perclass where tb_person.pcid=tb_perclass.pcid and pemail=@pemail and ppwd=@ppwd and pcname=@pcname", ConDB.GetOpen());
            cmd.Parameters.Add("@pemail", SqlDbType.VarChar,30).Value = per.Pemail;
            cmd.Parameters.Add("@ppwd", SqlDbType.VarChar,100).Value = per.Ppwd;
            cmd.Parameters.Add("@pcname", SqlDbType.VarChar,10).Value = clname;
             int x = Convert.ToInt32(cmd.ExecuteScalar());
             if (x > 0)
             {
                 result = "1";
             }
        }
        catch (Exception e)
        {
            result = "0";
        }
        finally {
            ConDB.Close();
        }
        return result;
    }
    public string getMyID(TBperson per, string clname)
    {
        string result = "";
        try
        {
            cmd = new SqlCommand("select tb_person.pid from tb_person,tb_perclass where tb_person.pcid=tb_perclass.pcid and pemail=@pemail and ppwd=@ppwd and pcname=@pcname", ConDB.GetOpen());
            cmd.Parameters.Add("@pemail", SqlDbType.VarChar, 30).Value = per.Pemail;
            cmd.Parameters.Add("@ppwd", SqlDbType.VarChar, 100).Value = per.Ppwd;
            cmd.Parameters.Add("@pcname", SqlDbType.VarChar, 10).Value = clname;
            string x = cmd.ExecuteScalar().ToString();
            if (!x.Equals(""))
            {
                result = x;
            }
        }
        catch (Exception e)
        {
            result = "";
        }
        finally
        {
            ConDB.Close();
        }
        return result;
    }
    public string ishave(string id,string pwd)
    {
        string result = "0";
        try
        {
            cmd = new SqlCommand("select count(*) from tb_person where ppwd=@ppwd and pid=@pid", ConDB.GetOpen());
            cmd.Parameters.Add("@ppwd", SqlDbType.VarChar, 100).Value = pwd;
            cmd.Parameters.Add("@pid", SqlDbType.VarChar, 20).Value = id;
            int x = Convert.ToInt32(cmd.ExecuteScalar());
            if (x > 0)
            {
                result = "1";
            }
        }
        catch (Exception e)
        {
            result = "0";
        }
        finally
        {
            ConDB.Close();
        }
        return result;
    }
    public string updatepwd(string id,string pwd)
    {
        string result = "0";
        try
        {
            cmd = new SqlCommand("update tb_person set ppwd=@ppwd where pid=@pid", ConDB.GetOpen());
            cmd.Parameters.Add("@ppwd", SqlDbType.VarChar, 100).Value = pwd;
            cmd.Parameters.Add("@pid", SqlDbType.VarChar, 20).Value = id;
            int x = cmd.ExecuteNonQuery();
            if (x > 0)
            {
                result = "1";
            }
        }
        catch (Exception e)
        {
            result = "0";
        }
        finally
        {
            ConDB.Close();
        }
        return result;
    }
 //   SqlTransaction tran;
    public void delper(string pid)//删除人员
    {
        Boolean result = true;
        try
        {

            cmd = new SqlCommand();
            cmd.Connection = ConDB.GetOpen();
            cmd.CommandText = "delperson";
            cmd.CommandType = CommandType.StoredProcedure;
//            tran = cmd.Connection.BeginTransaction();
 //           cmd.Transaction = tran;
            cmd.Parameters.Add("@pid", SqlDbType.VarChar,20).Value =pid;
            cmd.ExecuteNonQuery();
            //if (cmd.ExecuteNonQuery() > 0)
            //{
            //    result = false;
            //    tran.Commit();
            //}

        }
        catch (Exception e)
        {
            //result = true;
            //tran.Rollback();
        }
        finally
        {
            ConDB.Close();
        }
        //return result;
    }
    public string perfindpwd(string name,string email,string time)
    {
        string result = "0";
        try
        {
            cmd = new SqlCommand("select pid from tb_person where pemail=@pemail and pname=@pname", ConDB.GetOpen());
            cmd.Parameters.Add("@pemail", SqlDbType.VarChar, 30).Value = email;
            cmd.Parameters.Add("@pname", SqlDbType.VarChar, 10).Value = name;
           string pid=cmd.ExecuteScalar().ToString();
            pid=pid.Substring(0,7);
            if (pid.Equals(time)==true)
            {
                result = "1";
            }
        }
        catch (Exception e)
        {
            result = "0";
        }
        finally
        {
            ConDB.Close();
        }
        return result;
    }
    public string updateperfindpwd(string name, string email, string time)
    {
        string result = "0";
        try
        {
            if (perfindpwd(name, email, time).Equals("1"))
            {
                cmd = new SqlCommand("update tb_person set ppwd=@ppwd where tb_person.pemail=@pemail and pname=@pname", ConDB.GetOpen());
                cmd.Parameters.Add("@pemail", SqlDbType.VarChar, 30).Value = email;
                cmd.Parameters.Add("@ppwd", SqlDbType.VarChar, 100).Value = MD5.EncodeMD5("1");
                cmd.Parameters.Add("@pcname", SqlDbType.VarChar, 10).Value = name;
                int x = cmd.ExecuteNonQuery();
                if (x > 0)
                {
                    result = "1";
                }
            }
        }
        catch (Exception e)
        {
            result = "0";
        }
        finally
        {
            ConDB.Close();
        }
        return result;
    }
}
